var CookieDomain = "veuger.net";

var CookieExpiry = 7;


$(document).ready(function () {
    $("#myContent").flash({
        swf: "/flash/demo.swf",
        attributes: {
            id: "swfJvd",
            name: "swfJvd"
        },
        flashvars: {

        },
        params: {
            allowFullScreen: "true",
            allowScriptAccess: "always"
        },
        height: 200,
        width: 890
    });

    $("a[rel~=external]").attr("target", "_blank");

    $.gaTracker("UA-3906458-4");

    $("form#contact").submit(function(e) {
        e.preventDefault();
        //$("#contactResponse").addClass("loading");
        $("div#contactForm").addClass("send");
        var dataString = $("form#contact").serialize();

        //$("input").removeClass("error");
        //$("textarea").removeClass("error");

        $.ajax({
            type: "POST",
            url: "/contact/",
            data: dataString,
            dataType: "json",
            beforeSend: function(){
            },
            success: function(msg) {
                $("div#contactForm").removeClass("send");
                if(msg=="prima"){
                    $("#contactForm").html("Bedankt voor uw bericht! wij nemen contact met u op.");
                }else{

                    // Remove wrong set errors
                    
                    var cookie = $.cookie('contactForm');

                    if(cookie != null){
                        var oldErrors = cookie.split(",");

                        var newErrors = msg;

                        var rebuildNew = new Array();
                        for(i=0;i<newErrors.length;i++) {
                            rebuildNew[newErrors[i]] = newErrors[i];
                        }

                        var rebuildOld = new Array();
                        for(i=0;i<oldErrors.length;i++) {
                            rebuildOld[oldErrors[i]] = oldErrors[i];
                        }

                        for(i=0;i<oldErrors.length;i++) {
                            if(!(oldErrors[i] in rebuildNew) && (oldErrors[i] in rebuildOld)){
                                $("#"+oldErrors[i]).removeClass("error");
                            }
                        }

                        for(i=0;i<newErrors.length;i++) {
                            if($.inArray(newErrors[i], rebuildOld)){
                                $("#"+newErrors[i]).addClass("error");
                            }else{
                                $("#"+newErrors[i]).removeClass("error");
                            }
                        }

                    }else{
                        for(i=0;i<msg.length;i++) {
                            $("#"+msg[i]).addClass("error");
                        }
                    }

                    $.cookie('contactForm', msg, {
                        expires: CookieExpiry,
                        path: "/"
                    });
                }
            },
            error: function() {
                $("div#contactForm").removeClass("send");
                alert("jammer");
            }
        });
        return false;
    });

});


